package com.ysstech.reportworld.service.impl;

import com.ysstech.common.service.DicvalueService;
import com.ysstech.reportworld.entity.Table;
import com.ysstech.reportworld.entity.TableTable;
import com.ysstech.reportworld.enums.ReportWorldEnum;
import com.ysstech.reportworld.mapper.TableMapper;
import com.ysstech.reportworld.mapper.TableTableMapper;
import com.ysstech.reportworld.service.TableTableService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 集市层表依赖表信息 服务实现类
 * </p>
 *
 * @author lishuangliang
 * @since 2022-05-25
 */
@Service
public class TableTableServiceImpl extends ServiceImpl<TableTableMapper, TableTable> implements TableTableService {
    @Autowired
    private DicvalueService dicvalueService;
    @Autowired
    private TableMapper tableMapper;

    /**
     * 查询集市层依赖表集合信息
     *
     * @param table
     * @return
     */
    public List<Table> queryTableTable(Table table) throws Exception {
        List<Table> list = tableMapper.queryTableTable(table);
        // 报表世界所在分层
        return getReportTables(list);
    }

    /**
     * 查询字段依赖表集字段信息
     *
     * @param table
     * @return
     */
    public List<Table> queryTableTableDetail(Table table) throws Exception {
        List<Table> list = tableMapper.queryTableTableDetail(table);
        return getReportTables(list);
    }

    private List<Table> getReportTables(List<Table> list) throws Exception {
        // 报表世界所在分层
        Map<String, String> mapTableLayered = dicvalueService.getCacheDicValueMap(ReportWorldEnum.REPORT_TABLE_LAYERED.getCode());
        // 表类型
        Map<String, String> mapFtype = dicvalueService.getCacheDicValueMap(ReportWorldEnum.REPORT_TABLE_FTYPE.getCode());
        // 表来源
        Map<String, String> maptFsource = dicvalueService.getCacheDicValueMap(ReportWorldEnum.REPORT_TABLE_FSOURCE.getCode());
        for (Table table : list) {
            //报表世界所在分层
            if (null != mapTableLayered && mapTableLayered.containsKey(String.valueOf(table.getLayered()))) {
                table.setStrLayered(mapTableLayered.get(String.valueOf(table.getLayered())));
            }
            //表类型
            if (null != mapFtype && mapFtype.containsKey(String.valueOf(table.getFtype()))) {
                table.setStrFtype(mapFtype.get(String.valueOf(table.getFtype())));
            }
            //表来源
            if (null != maptFsource && maptFsource.containsKey(String.valueOf(table.getFsource()))) {
                table.setStrFsource(maptFsource.get(String.valueOf(table.getFsource())));
            }
        }
        return list;
    }
}
